const oUser = $('.username');
const oPwd = $('.password');
let oBtn = $('.register');
let tip = $('.error');
let tipOne = $('.error1');



// 1.判断每个框的状态
let isUser = false, isPwd = false;

// 2.用户名绑定input事件
oUser.on('input', function () {
  let username = oUser.val().trim();
  let res = username;
  if (/^.{0}$/.test(res)) {
    tip.html('用户名不能为空');
    tip.css('color', 'red')
    isUser = false;
  } else if (!/^.{3,10}$/.test(res)) {
    tip.html('用户名的长度不对');
    tip.css('color', 'red')
    isUser = false;
  } else if (/^\d/.test(res)) {
    tip.html('数字不能开头');
    tip.css('color', 'red')
    isUser = false;
  } else if (!/^[a-zA-Z0-9_\u4e00-\u9fa5]{3,10}$/.test(res)) {
    tip.html('只能由数字字母下划线中文组成');
    tip.css('color', 'red')
    isUser = false;
  } else {
    tip.html('√');
    tip.css('color', 'green')
    isUser = true;
  }
  if (isUser && isPwd) {
    oBtn.prop({ 'disabled': false });
  } else {
    oBtn.prop({ 'disabled': true });
  }
})

// 3.密码框的判断
oPwd.on('input', function () {
  let pas = oPwd.val().trim();
  if (!/./.test(pas)) {
    tipOne.html('密码不能为空');
    tipOne.css('color', 'red');
    isPwd = false;
  } else if (!/^.{4,10}$/.test(pas)) {
    tipOne.html('密码长度不对');
    tipOne.css('color', 'red');
    isPwd = false;
  } else if (!/^[a-zA-Z0-9_]{4,10}$/.test(pas)) {
    tipOne.html('只能由数字字母下划线组成');
    tipOne.css('color', 'red')
    isPwd = false;
  } else {
    let flag1 = 0, flag2 = 0, flag3 = 0;
    if (/\d/.test(pas)) {
      flag1 = 1;
    }
    if (/[a-z]/.test(pas)) {
      flag2 = 1;
    }
    if (/[A-Z]/.test(pas)) {
      flag3 = 1;
    }
    let res = flag1 + flag2 + flag3;
    if (res === 1) {
      tipOne.html('密码强度弱');
    }
    else if (res === 2) {
      tipOne.html('密码强度中');
    }
    else if (res === 3) {
      tipOne.html('密码强度高');
    }
    tipOne.css('color', 'green');
    isPwd = true;
  }
  if (isUser && isPwd) {
    console.log(666);
    oBtn.prop({ 'disabled': false });
  } else {
    oBtn.prop({ 'disabled': true });

  }
}
)

// 4.注册的点击事件
oBtn.click(function () {
  let username = oUser.val();
  let password = oPwd.val();
  localStorage.setItem(username, password);
  if (isUser && isPwd) {
    oBtn.prop({ 'disabled': false });
    // 1.数据请求
    register({
      username,
      password,
    }).then(res => {
      const { status, msg } = res;
      if (status) {
        location.href = '../html/3.login.html';
      } else {
        alert(msg);
      }
    })
  } else {
    oBtn.prop({ 'disabled': true });
  }
})